package a2;

import java.util.ArrayList;

/**
 * Directory is a class that represents a single Folder/Directory on a virtual
 * {@link FileSystem}. It has a list of {@link Item}s which contain the sub
 * directories and files within this {@link Directory}. Handles the "ls" bash
 * command.
 * 
 * @author Dylan Seago, Tianran Liu, Hilal Dib, Paschal Oduoza
 */
public class Directory extends Item {

	/**
	 * Array of {@link Item}s that are contained within this {@link Directory}.
	 */
	private ArrayList<Item> children = new ArrayList<Item>();

	/**
	 * Initializes a new {@link Directory} with a given {@code name}. and
	 * {@code parent} {@link Directory}.
	 * 
	 * @param name
	 *            Name of this {@link Directory}.
	 * @param parent
	 *            {@link Directory} that contains this {@link Directory}.
	 */
	public Directory(String name, Directory parent) {
		super(name, parent);
	}

	/**
	 * Prints out all the names of the {@code Directories} (prepended with a
	 * space) followed by the names of all the {@code Files} within this
	 * {@link Directory}.
	 */
	@Override
	public String ls() {
		String out = getName() + ":" + "\n";
		for (Item item : children)
			if (item instanceof Directory)
				out += item.getName() + "/" + "\n";
		
		for (Item item : children)
			if (item instanceof File)
				out += item.getName() + "\n";
		out += "\n";
		return out;
	}

	/**
	 * Gets the child {@link Item}s within this directory. This includes all
	 * {@code Directories} and {@code Files}.
	 * 
	 * @return ArrayList of child items.
	 */
	public ArrayList<Item> getChildren() {
		return children;
	}

	/**
	 * Finds and returns the child {@link Item} specified by its name contained
	 * within this {@link Directory}.
	 * 
	 * @param name
	 *            Name of {@link Item}.
	 * @return {@link Item} object with specified {@code name}.
	 */
	public Item getChild(String name) {
		for (Item dir : children) {
			if (dir.getName().equals(name)) {
				return dir;
			}
		}
		return null;
	}
	

}
